using System;
using Microsoft.SPOT;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace eHouse
{
    class ConnectionManager
    {
        private static ConnectionManager instance;
        private string ip = "192.168.1.2";
        private int port = 6969;
        private SocketManager manager;

        public static ConnectionManager GetInstance()
        {
            if (instance == null)
                instance = new ConnectionManager();

            return instance;
        }

        private ConnectionManager()
        {
            this.manager = new SocketManager(ip,port);
        }

        public void CloseConnection()
        {
            try
            {
                manager.CloseConnection();
            }
            catch (Exception) { }
        }

        public void AddLightRules(string rule)
        {
            Byte[] response = manager.Send("SET_RULES_LIGHTS/"+rule);
        }

        public void AddIrrigationRules(string rule)
        {
            Byte[] response = manager.Send("SET_RULES_IRRIGATION/" + rule);
        }

        public void AddTempRules(string rule)
        {
            Byte[] response = manager.Send("SET_RULES_TEMPERATURE/" + rule);
        }

        public string GetLightRules()
        {
            Byte[] response = manager.Send("GET_RULES_LIGHTS");

            string rules = new String(Encoding.UTF8.GetChars(response));
            return rules;
        }

        public string GetIrrigationRules()
        {
            Byte[] response = manager.Send("GET_RULES_IRRIGATION");

            string rules = new String(Encoding.UTF8.GetChars(response));
            return rules;
        }

        public string GetTempRules()
        {
            Byte[] response = manager.Send("GET_RULES_TEMPERATURE");

            string rules = new String(Encoding.UTF8.GetChars(response));
            return rules;
        }

        public void DeleteLightRule(string guid)
        {
            Byte[] response = manager.Send("DELETE_RULES_LIGHTS/"+guid);

        }

        public void DeleteIrrigationRule(string guid)
        {
            Byte[] response = manager.Send("DELETE_RULES_IRRIGATION/" + guid);
        }

        public void DeleteTempRule()
        {
            Byte[] response = manager.Send("DELETE_RULES_TEMPERATURE");
        }

        public void SendNotification(string msg)
        {
            manager.Send("SEND_NOTIFICATION/" + msg);
        }
    }

    class SocketManager
    {
        
        private Socket socket;
       
        private byte[] buffer;
        private byte[] bufferResponse;

        
        public SocketManager(string ip, int port)
        {
            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
        }


        public byte[] Send(string text)
        {
            buffer = Encoding.UTF8.GetBytes(text);
            bufferResponse = new byte[1024];
            socket.Send(buffer);
            if (socket.Poll(-1, SelectMode.SelectRead))
            {
                socket.Receive(bufferResponse, socket.Available, SocketFlags.None);
            }
            return bufferResponse;
        }

        public void CloseConnection()
        {            
                this.Send("QUIT");
                socket.Close();            
        }
    }
   
}
